package com.ztx.jpaTest;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MainTest {

    public static void main(String[] args) {
        // 1.创建EntityManagerFactory
        String persistenceUnitName = "jpa-test-1";

        Map<String, Object> propertiesMap = new HashMap<>();
        propertiesMap.put("hibernate.format_sql", false);

        EntityManagerFactory entityManagerFactory =
                Persistence.createEntityManagerFactory(persistenceUnitName,
                propertiesMap);

        // 2.创建EntityManager
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        // 3.开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        // 4.进行持久化操作
        Customer customer = new Customer();
        customer.setAge(12);
        customer.setEmail("tom@qq.com");
        customer.setLastName("Tom");
        customer.setBirth(new Date());
        customer.setCreateTime(new Date());

        entityManager.persist(customer);

        // 5.提交事务
        transaction.commit();

        // 6.关闭EntityManager
        entityManager.close();

        // 7.关闭EntityManagerFactory
        entityManagerFactory.close();

    }

}
